package com.bbzn.device.client.controller;

import com.bbzn.device.client.vo.req.PlanLeaderVO;
import com.bbzn.device.client.aspect.OperationAnnotation;
import com.bbzn.device.client.constant.*;
import com.bbzn.device.client.dataobject.Leader;
import com.bbzn.device.client.dataobject.PlanLeader;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.dto.index.PlanLeaderDTO;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.*;
import com.bbzn.device.client.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Author wulongbo
 * @Date 2022/1/18 16:12
 * @Version 1.0
 */

@RestController
@RequestMapping("/pl")
@Slf4j
public class LeaderPlanController extends BaseApiService {

    @Autowired
    private CommonService commonService;

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private PlanLeaderService planLeaderService;

    @Autowired
    private WorkPlanService workPlanService;

    @Autowired
    private LeaderService leaderService;

    /**
     * 给计划添加已有负责人
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addLeaderForPlan", method = RequestMethod.POST)
    @OperationAnnotation(content = "给计划添加已有负责人")
    public BaseResponse addLeaderForPlan(@RequestBody PlanLeaderVO planLeaderVO, HttpServletRequest request) throws Exception {
        Long userId = commonService.getUserId(request);
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        String username = MayiktJwtUtils.getUsername(request, secretKey);
        if (!OptionalBean.ofNullable(planLeaderVO.getPlanId()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(PlanConstant.PLAN_ENUM, PlanConstant.PLAN_UN_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(planLeaderVO.getLeaderId()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(LeaderConstant.LEADER_ENUM, LeaderConstant.LEADER_UN_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }










        int count = workPlanService.selectCountPrimaryKey(planLeaderVO.getPlanId());
        if (count == 0) {
            SystemParam systemParam = systemParamByCacheService.getValues(PlanConstant.PLAN_ENUM, PlanConstant.PLAN_ID_NOT_EXIST_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        count = leaderService.selectCountPrimaryKey(planLeaderVO.getLeaderId());
        if (count == 0) {
            SystemParam systemParam = systemParamByCacheService.getValues(LeaderConstant.LEADER_ENUM, LeaderConstant.LEADER_ID_NOT_EXIST_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        PlanLeader pl = new PlanLeader();
        pl.setLeaderId(planLeaderVO.getLeaderId());
        pl.setPlanId(planLeaderVO.getPlanId());
        pl.setUserId(userId);
        pl.setCreateTime(new Date());
        pl.setCreatedBy(username);
        planLeaderService.insertSelective(pl);
        SystemParam systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.ADD_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 给计划添加新负责人
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addNewLeaderForPlan", method = RequestMethod.POST)
    @OperationAnnotation(content = "给计划添加新负责人")
    public BaseResponse addNewLeaderForPlan(@RequestBody PlanLeaderVO planLeaderVO, HttpServletRequest request) throws Exception {
        Long userId = commonService.getUserId(request);
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        String username = MayiktJwtUtils.getUsername(request, secretKey);
        if (!OptionalBean.ofNullable(planLeaderVO.getPlanId()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(PlanConstant.PLAN_ENUM, PlanConstant.PLAN_UN_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        int count = workPlanService.selectCountPrimaryKey(planLeaderVO.getPlanId());
        if (count == 0) {
            SystemParam systemParam = systemParamByCacheService.getValues(PlanConstant.PLAN_ENUM, PlanConstant.PLAN_ID_NOT_EXIST_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(planLeaderVO.getName()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(LeaderConstant.LEADER_ENUM, LeaderConstant.LEADER_UN_NAME_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(planLeaderVO.getPhone()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(UserConstant.USER_ENUM, UserConstant.PHO_UN_PHONE_PREFIX + language);
            throw new DefinitException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(planLeaderVO.getPostId()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(LeaderConstant.LEADER_ENUM, LeaderConstant.LEADER_UN_POST_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(planLeaderVO.getAge()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(LeaderConstant.LEADER_ENUM, LeaderConstant.LEADER_UN_AGE_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(planLeaderVO.getSex()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(LeaderConstant.LEADER_ENUM, LeaderConstant.LEADER_UN_SEX_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(planLeaderVO.getTitle()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(LeaderConstant.LEADER_ENUM, LeaderConstant.LEADER_UN_TITLE_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(planLeaderVO.getImg()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(UserConstant.USER_ENUM, UserConstant.USER_UN_IMG_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        if (!PhoneFormatCheckUtils.isPhoneLegal(planLeaderVO.getPhone())) {
            SystemParam systemParam = systemParamByCacheService.getValues(UserConstant.USER_ENUM, UserConstant.PHO_NUM_PREFIX + language);
            return setResultError(systemParam.getCode(), systemParam.getMassage());
        }
        // 手机号 不得重复
        Leader query = new Leader();
        query.setPhone(planLeaderVO.getPhone());
        int result = leaderService.selectCountBySelective(query);
        if (result > 0) {
            SystemParam systemParam = systemParamByCacheService.getValues(UserConstant.USER_ENUM, UserConstant.PHO_PWD_NOT_NATCH_PREFIX + language);
            return setResultError(systemParam.getCode(), systemParam.getMassage());
        }

        Leader leader = BabaBeanUtils.voToDto(planLeaderVO, Leader.class);
        leader.setUserId(userId);
        leader.setCreateTime(new Date());
        leader.setCreatedBy(username);
        leaderService.insertSelective(leader);

        PlanLeader pl = new PlanLeader();
        pl.setLeaderId(leader.getId());
        pl.setPlanId(planLeaderVO.getPlanId());
        pl.setUserId(userId);
        pl.setCreateTime(new Date());
        pl.setCreatedBy(username);
        planLeaderService.insertSelective(pl);
        SystemParam systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.ADD_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }


    /**
     * 计划删除负责人
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delLeaderForPlan", method = RequestMethod.POST)
    @OperationAnnotation(content = "计划删除负责人")
    public BaseResponse delLeaderForPlan(@RequestBody PlanLeaderVO planLeaderVO, HttpServletRequest request) throws Exception {
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        /**
         * 计划负责人主键id
         */
        if (!OptionalBean.ofNullable(planLeaderVO.getId()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(CustomerConstant.CUSTOMER_ENUM, CustomerConstant.CUSTOMER_UN_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        planLeaderService.deleteByPrimaryKey(planLeaderVO.getId());
        SystemParam systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.DELETE_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }


    /**
     * 查询计划以及对应的负责人
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getLeaderForPlan", method = RequestMethod.POST)
    @OperationAnnotation(content = "查询计划以及对应的负责人")
    public BaseResponse getLeaderForPlan(HttpServletRequest request) {
        Long userId = commonService.getUserId(request);
        List<PlanLeaderDTO> planLeaderList = planLeaderService.getPlanLeaderList(userId);
        return setResultSuccess(planLeaderList);
    }

}
